/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.kmxd.ams.core.oss.template;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.common.enums.OssEnum;
import com.kmxd.ams.core.exception.BizException;
import com.kmxd.ams.core.oss.bo.OssFile;
import com.kmxd.ams.core.oss.properties.AttachmentSubPath;
import com.kmxd.ams.core.oss.properties.OssProperties;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

/**
 * 对外公布接口
 *
 * <p>方便点直接实现IOssSvc
 *
 * @author ZuoLG
 */
@RequiredArgsConstructor
public class OssTemplate implements InitializingBean, IOssSvc {

  /** 配置文件 */
  private final OssProperties ossProperties;

  /** 所有实现 */
  @Autowired private Map<String, IOssSvc> serviceMap;

  private IOssSvc ossSvc;

  @Override
  @SneakyThrows
  public void afterPropertiesSet() {
    ossSvc = serviceMap.get(ossProperties.getUploadType());
  }

  @Override
  public OssFile upload(MultipartFile file, OssEnum ossEnum, String path) throws Exception {
    return ossSvc.upload(file, ossEnum, path);
  }

  @Override
  public OssFile upload(File file, OssEnum ossEnum, String path) throws Exception {
    return ossSvc.upload(file, ossEnum, path);
  }

  @Override
  public OssFile upload(MultipartFile file, OssEnum ossEnum, String path, String newFileName)
      throws Exception {
    return ossSvc.upload(file, ossEnum, path, newFileName);
  }

  @Override
  public OssFile upload(File file, OssEnum ossEnum, String path, String newFileName)
      throws Exception {
    return ossSvc.upload(file, ossEnum, path, newFileName);
  }

  @Override
  public boolean delete(String path) {
    return ossSvc.delete(path);
  }

  @Override
  public String rename(String path, String newName) {
    return ossSvc.rename(path, newName);
  }

  @Override
  public File getFile(String path) {
    return ossSvc.getFile(path);
  }

  @Override
  public File copy(String src, String target) throws Exception {
    return ossSvc.copy(src, target);
  }

  @Override
  public InputStream getFileInputStream(String path) {
    return ossSvc.getFileInputStream(path);
  }

  @Override
  public String checkFileMd5(String md5, String fileName) {
    return ossSvc.checkFileMd5(md5, fileName);
  }

  @Override
  public OssFile exportZip(List<OssFile> paths) {
    return ossSvc.exportZip(paths);
  }

  /** 公共附件文件路径 */
  public String getAttachmentPath(String defPath) {
    return getRootPath(defPath) + ossProperties.getAttachment();
  }
  /** 收集整编库 */
  public String getCollectPath(String defPath) {
    return getRootPath(defPath) + ossProperties.getCollect();
  }
  /** 归档库 */
  public String getManagePath(String defPath) {
    return getRootPath(defPath) + ossProperties.getManage();
  }
  /** 临时文件 */
  public String getTempPath() {
    return getRootPath(null) + ossProperties.getTemp();
  }

  public String getBackupPath(String defPath) {
    return getRootPath(defPath) + ossProperties.getBackup();
  }

  public String getAipPath(String defPath) {
    return getRootPath(defPath) + ossProperties.getAip();
  }

  public String getOcrPath(String defPath) {
    return getRootPath(defPath) + ossProperties.getOcr();
  }

  /**
   * 获取当前默认使用的路径key
   *
   * @return
   */
  public String getDefPath() {
    return ossProperties.getDefPath();
  }

  public AttachmentSubPath getAttachmentSubPath() {
    return ossProperties.getAttachmentSubPath();
  }

  /**
   * 根据类型获取使用磁盘路径
   *
   * @param defPath
   * @return
   */
  public String getRootPath(String defPath) {
    Map<String, String> paths = ossProperties.getPaths();
    return MapUtil.getStr(
        paths, ObjectUtil.isNotEmpty(defPath) ? defPath : ossProperties.getDefPath());
  }
  /**
   * 通过条目状态，获取文件路径
   *
   * @param status 条目状态
   * @return 路径
   */
  public String getArrangeFilePath(Integer status) {
    switch (status) {
      case 0:
        return this.getCollectPath(null);
      case 1:
        return this.getManagePath(null);
      default:
        throw BizException.instance(ErrorCode.NOT_FOUND, "未找到该状态对应的路径信息：" + status);
    }
  }

  /**
   * 根据状态值获取路径
   *
   * @param statusVal
   * @return
   */
  public String getPath(Integer statusVal) {
    return getArrangeFilePath(statusVal);
  }

  @Override
  public boolean exist(String path) {
    return ossSvc.exist(path);
  }

  @Override
  public String separator() {
    return ossSvc.separator();
  }

  @Override
  public boolean mkdir(String dest) {
    return ossSvc.mkdir(dest);
  }

  @Override
  public String getSystemTempPath() {
    return ossSvc.getSystemTempPath();
  }
}
